
const { init } = require('./../../wxCloudClientSDK.umd.js')
// 指定云开发环境 ID
wx.cloud.init({
  env: "cloud1-9gel6ndh8e555155", // 当前的云开发环境 ID
});

const client = init(wx.cloud);


Page({
  data: {
    memberLevels: []
  },

  onLoad() {
    // 页面加载时的初始化逻辑
    this.queryMemberLevelInfos();
  },

  /* 查询会员级别信息 */
  queryMemberLevelInfos: function() {

    client.models.membership_levels.list({
      filter: {
        where: {

        },
      }
    }).then(res => {
      const data = res.data.records;
      data.sort((a, b) => b.price - a.price)
      this.parseMembershipLevelData(data);
    }).catch(res => {
      console.log("query membership_levels err: ", res);
    });

  },

  /* 解析会员级别信息 */
  parseMembershipLevelData: function(membershipLevelInfos) {

    const membershipLevel = membershipLevelInfos.map(item => {
      return {
        id: item._id,
        name: item.levelName,
        image: item.cardImageUrl,
        benefits: item.benefits,
        price: item.price,
        duration: item.duration,
        durationUnit: item.durationUnit
      };
    });

    this.setData({
      memberLevels: membershipLevel
    });
  },

  // 查看会员详情
  viewMemberDetail(e) {
    const level = e.currentTarget.dataset.level;
    wx.navigateTo({
      url: `/pages/memberdetail/memberdetail?level=${level}`
    });
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  }
}); 